package com.opener.web.model.sys;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.opener.common.model.Model;

import java.io.IOException;
import java.util.*;

/**
 *
 * @author baboy
 * @date 07/12/2016
 */
public class SysConf extends Model {

    private static final long serialVersionUID = -3373025212343942445L;
    public static String MODULE_SYS = "sys";
    public static String MODULE_CLIENT = "client";

    public static class ValueType{
        public static String STRING = "String";
        public static String INT = "Integer";
        public static String LONG = "Long";
        public static String FLOAT = "Float";
        public static String LIST = "List";
        public static String MAP = "HashMap";
    }
    private Integer id;
    private String module;
    private String title;
    private String note;
    private String key;
    private String value;
    private String valueType;
    private Integer createUid;
    private Date createTime;
    private Integer updateUid;
    private Date updateTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getValueType() {
        return valueType;
    }

    public void setValueType(String valueType) {
        this.valueType = valueType;
    }

    public Integer getCreateUid() {
        return createUid;
    }

    public void setCreateUid(Integer createUid) {
        this.createUid = createUid;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getUpdateUid() {
        return updateUid;
    }

    public void setUpdateUid(Integer updateUid) {
        this.updateUid = updateUid;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Object getTypedValue(){
        if (value == null){
            return value;
        }
        Object val = value;
        if (ValueType.INT.equalsIgnoreCase(valueType)){
            val = Integer.parseInt(value);
        }
        if (ValueType.LONG.equalsIgnoreCase(valueType)){
            val = Long.parseLong(value);
        }
        if (ValueType.FLOAT.equalsIgnoreCase(valueType)){
            val = Float.parseFloat(value);
        }
        if (ValueType.MAP.equalsIgnoreCase(valueType) ||
                ValueType.LIST.equalsIgnoreCase(valueType)){
            ObjectMapper mapper = new ObjectMapper();
            try {
                if(value.startsWith("[")){
                    val = mapper.readValue(value, ArrayList.class);
                }else if(value.startsWith("{")){
                    val = mapper.readValue(value, HashMap.class);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return val;
    }
    public Map getMapValue() {
        Object val = getTypedValue();
        if (val != null && val instanceof Map){
            return (Map)val;
        }
        return null;
    }

    public List getListValue() {
        Object val = getTypedValue();
        if (val != null && val instanceof List){
            return (List)val;
        }
        return null;
    }

    public Long getLongValue() {
        Object val = getTypedValue();
        if (val != null){
            try {
                Long v = Long.parseLong(val.toString());
                return v;
            }catch (Exception e){

            }
        }
        return null;
    }

    public Integer getIntegerValue() {
        Object val = getTypedValue();
        if (val != null){
            try {
                Integer v = Integer.parseInt(val.toString());
                return v;
            }catch (Exception e){

            }
        }
        return null;
    }

    public Float getFloatValue() {
        Object val = getTypedValue();
        if (val != null){
            try {
                Float v = Float.parseFloat(val.toString());
                return v;
            }catch (Exception e){

            }
        }
        return null;
    }
}
